package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.StbCompany;
import com.ruoyi.system.domain.StbIntellectualProperty;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.IStbCompanyService;
import com.ruoyi.system.service.IStbIntellectualPropertyService;
import com.ruoyi.system.service.ISysDictDataService;

/**
 * 知识产权Controller
 * 
 * @author ruoyi
 * @date 2020-02-08
 */
@Controller
@RequestMapping("/system/property")
public class StbIntellectualPropertyController extends BaseController
{
    private String prefix = "system/property";

    @Autowired
    private IStbIntellectualPropertyService stbIntellectualPropertyService;
    @Autowired
	private IStbCompanyService stbCompanyService;
    
    @Autowired
    private ISysDictDataService dictDataService;
    
    @RequiresPermissions("system:property:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<StbIntellectualProperty> util = new ExcelUtil<StbIntellectualProperty>(StbIntellectualProperty.class);
        return util.importTemplateExcel("知识产权数据");
    }
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
    	ExcelUtil<StbIntellectualProperty> util = new ExcelUtil<StbIntellectualProperty>(StbIntellectualProperty.class);
    	List<StbIntellectualProperty> propertyList = util.importExcel(file.getInputStream());
    	String operName = ShiroUtils.getSysUser().getLoginName();
    	String message = stbIntellectualPropertyService.importProperty(propertyList, updateSupport, operName);
    	return AjaxResult.success(message);
    }

    @RequiresPermissions("system:property:view")
    @GetMapping()
    public String property(ModelMap mmap)
    {
    	StbCompany stbCompany = new  StbCompany();
    	List<StbCompany> list = stbCompanyService.selectStbCompanyList(stbCompany);
    	mmap.put("companyList",list);
        return prefix + "/property";
    }
    
	/*
	 * 查询知识产权  time:时间  num:产权数量
	 */
    @PostMapping("/propertypecount")
    @ResponseBody
    public List<Map<String,Object>> selectintellectualpropertyCount(){
        StbIntellectualProperty stbIntellectualProperty = new StbIntellectualProperty();
        List<Map<String, Object>> intellectualpropertyList = stbIntellectualPropertyService.selectintellectualpropertyCount(stbIntellectualProperty);
        return intellectualpropertyList;
    }

	/*
	 * 查询各类知识产权数量  name:名称 num:数量
	 */
    @PostMapping("/iprotypecount")
    @ResponseBody
    public List<Map<String, Object>> selectintellectualpropertytypeCount(){
    	SysDictData dictData=new SysDictData();
    	dictData.setDictType("intellectual_property_type");
    	List<SysDictData> list = dictDataService.selectDictDataList(dictData);
    	List<Map<String, Object>> resultMapList = new LinkedList<Map<String,Object>>();
    	StbIntellectualProperty stbIntellectualProperty = new StbIntellectualProperty();
    	List<StbIntellectualProperty> iproList = stbIntellectualPropertyService.selectStbIntellectualPropertyList(stbIntellectualProperty);
    	for (SysDictData sysDictData : list) {
    		Map<String,Object> map=new HashMap<String,Object>();
			map.put("name", sysDictData.getDictLabel());
			Integer num=0;
			String key=sysDictData.getDictValue();
			for (StbIntellectualProperty item : iproList) {
				String iproTabs = item.getIproType();
				if(iproTabs!=null&&!iproTabs.equals("")) {
					String[] keyArr = iproTabs.split(",");
					List<String> keyList= new ArrayList<>(Arrays.asList(keyArr));
					if(keyList.contains(key)) {
						num++;
					}
				}
			}
			map.put("num",num);
			resultMapList.add(map);
		}
    	return resultMapList;
    }
    
    /**
     * 查询知识产权列表
     */
    @RequiresPermissions("system:property:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StbIntellectualProperty stbIntellectualProperty)
    {
        startPage();
        List<StbIntellectualProperty> list = stbIntellectualPropertyService.selectStbIntellectualPropertyList(stbIntellectualProperty);
        return getDataTable(list);
    }

    /**
     * 导出知识产权列表
     */
    @RequiresPermissions("system:property:export")
    @Log(title = "知识产权", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StbIntellectualProperty stbIntellectualProperty)
    {
        List<StbIntellectualProperty> list = stbIntellectualPropertyService.selectStbIntellectualPropertyList(stbIntellectualProperty);
        ExcelUtil<StbIntellectualProperty> util = new ExcelUtil<StbIntellectualProperty>(StbIntellectualProperty.class);
        return util.exportExcel(list, "property");
    }

    /**
     * 新增知识产权
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	StbCompany stbCompany = new  StbCompany();
    	List<StbCompany> list = stbCompanyService.selectStbCompanyList(stbCompany);
    	mmap.put("companyList",list);
        return prefix + "/add";
    }

    /**
     * 新增保存知识产权
     */
    @RequiresPermissions("system:property:add")
    @Log(title = "知识产权", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StbIntellectualProperty stbIntellectualProperty)
    {
    	if(stbIntellectualProperty.getIproCode()!=null&&!stbIntellectualProperty.getIproCode().equalsIgnoreCase("")) {
    		//1.如果知识产权名称或代码存在就不能添加  2.如果代码与名称同时不存在即可添加
        	StbIntellectualProperty obj = new StbIntellectualProperty();
        	obj.setIproCode(stbIntellectualProperty.getIproCode());
        	//StbIntellectualProperty obj1 = new StbIntellectualProperty();
        	//obj1.setIproName(stbIntellectualProperty.getIproName());
        	List<StbIntellectualProperty> iproList = stbIntellectualPropertyService.selectStbIntellectualPropertyList(obj);
        	//List<StbIntellectualProperty> iproList1 = stbIntellectualPropertyService.selectStbIntellectualPropertyList(obj1);
        	if((iproList!=null&&iproList.size()>0)/*||(iproList1!=null&&iproList1.size()>0)*/) {
    	        return AjaxResult.error("该知识产权代码已存在");
        	}
    	}
    	stbIntellectualProperty.setCreateBy(ShiroUtils.getLoginName());
    	stbIntellectualProperty.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(stbIntellectualPropertyService.insertStbIntellectualProperty(stbIntellectualProperty));
    }

    /**
     * 修改知识产权
     */
    @GetMapping("/edit/{iproId}")
    public String edit(@PathVariable("iproId") Long iproId, ModelMap mmap)
    {
    	StbCompany stbCompany = new  StbCompany();
    	List<StbCompany> list = stbCompanyService.selectStbCompanyList(stbCompany);
    	mmap.put("companyList",list);
        StbIntellectualProperty stbIntellectualProperty = stbIntellectualPropertyService.selectStbIntellectualPropertyById(iproId);
        mmap.put("stbIntellectualProperty", stbIntellectualProperty);
        return prefix + "/edit";
    }

    /**
     * 修改保存知识产权
     */
    @RequiresPermissions("system:property:edit")
    @Log(title = "知识产权", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StbIntellectualProperty stbIntellectualProperty)
    {
    	stbIntellectualProperty.setUpdateBy(ShiroUtils.getLoginName());
    	if(stbIntellectualProperty.getIproCode()!=null&&!stbIntellectualProperty.getIproCode().equalsIgnoreCase("")) {
    		//1.如果知识产权名称或代码存在就不能修改（不是它自己的）  2.如果代码与名称同时不存在即可修改
        	StbIntellectualProperty obj = new StbIntellectualProperty();
        	obj.setIproCode(stbIntellectualProperty.getIproCode());
        	//StbIntellectualProperty obj1 = new StbIntellectualProperty();
        	//obj1.setIproName(stbIntellectualProperty.getIproName());
        	List<StbIntellectualProperty> iproList = stbIntellectualPropertyService.selectStbIntellectualPropertyList(obj);
        	//List<StbIntellectualProperty> iproList1 = stbIntellectualPropertyService.selectStbIntellectualPropertyList(obj1);
        	int flag=0;
        	if((iproList==null||iproList.size()==0)/*||(iproList1==null&&iproList1.size()==0)*/) {
        		flag=1;
        	}else if(iproList.size()==1) {
        		StbIntellectualProperty stbIntellectualProperty2 = iproList.get(0);
        		if(stbIntellectualProperty2.getIproId().equals(stbIntellectualProperty.getIproId())) {
        			//flag=1;
        			//if(iproList1.size()==1) {
        	    		//StbIntellectualProperty stbIntellectualProperty3 = iproList1.get(0);
        	    		//if(stbIntellectualProperty3.getIproId().equals(stbIntellectualProperty.getIproId())) {
        	    			flag=1;
        	    		}else {
        	    			flag=2;
        	    		}
        	    	}
        		//}else {
        			//flag=2;
        		//}
        	//}else if(iproList1.size()==1) {
        		//StbIntellectualProperty stbIntellectualProperty3 = iproList1.get(0);
        		//if(stbIntellectualProperty3.getIproId().equals(stbIntellectualProperty.getIproId())) {
        			//flag=1;
        		//}else {
        			//flag=2;
        		//}
        	//}
        	if(flag==1) {
        		return toAjax(stbIntellectualPropertyService.updateStbIntellectualProperty(stbIntellectualProperty));
        	}else if(flag==2) {
        		return AjaxResult.error("知识产权代码已存在");
        	}else {
        		return AjaxResult.error("知识产权代码一经确定不可更改!");
        	}
    	}
    	return toAjax(stbIntellectualPropertyService.updateStbIntellectualProperty(stbIntellectualProperty));
    	
    }

    /**
     * 删除知识产权
     */
    @RequiresPermissions("system:property:remove")
    @Log(title = "知识产权", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(stbIntellectualPropertyService.deleteStbIntellectualPropertyByIds(ids));
    }
}
